package com.waiter.moximoxi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.waiter.moximoxi.R;
import com.waiter.moximoxi.bean.SendingOrderBean;
import com.waiter.moximoxi.net.API;
import com.waiter.moximoxi.utils.JsonTwoUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.yhong.aframework.adapter.ZmAdapter;
import cn.yhong.aframework.adapter.ZmHolder;
import cn.yhong.aframework.dialog.BaseDialog;
import cn.yhong.aframework.listener.OkHttpSuccessListener;
import cn.yhong.aframework.listener.OnCallbackListener;
import cn.yhong.aframework.utils.AtyUtils;
import cn.yhong.aframework.utils.MyOkHttpUtils;
import cn.yhong.aframework.view.IBaseView;

/**
 * Created by 17639 on 2020/10/14.
 */

public class SendingOrderDialog extends BaseDialog implements View.OnClickListener {

    private ImageView dialog_sending_dis;
    private ListView sending_listview;
    private TextView total_count_tv;
    private TextView total_price_tv;
    private TextView check_out_tv;

    private List<SendingOrderBean.ListBean> sendMenuBeans;
    private SendingOrderAdapter sendingOrderAdapter;

    public SendingOrderDialog(Context context, OnCallbackListener onCallbackListener) {
        super(context, onCallbackListener);
    }

    @Override
    public Dialog initDialog(Context context) {
        View view = View.inflate(context, R.layout.dialog_sending_order, null);
        dialog_sending_dis = view.findViewById(R.id.dialog_sending_dis);
        sending_listview = view.findViewById(R.id.sending_listview);
        total_count_tv = view.findViewById(R.id.total_count_tv);
        total_price_tv = view.findViewById(R.id.total_price_tv);
        check_out_tv = view.findViewById(R.id.check_out_tv);

        dialog_sending_dis.setOnClickListener(this);
        check_out_tv.setOnClickListener(this);

        sendMenuBeans = new ArrayList<>();
        sendingOrderAdapter = new SendingOrderAdapter(context, sendMenuBeans);
        sending_listview.setAdapter(sendingOrderAdapter);

        return initMatchWrapDialog(view, context, Gravity.CENTER, android.R.style.Animation_InputMethod);
    }


    public void showSendDialog(int shop_id) {
        Map<String, String> params = API.getParams();
        params.put("id", shop_id + "");
        MyOkHttpUtils.post_CallBack(API.order_goods, params, API.getHeads(context), new OkHttpSuccessListener(
                (IBaseView) context, "送餐状态", 2) {
            @Override
            public void onSuccessResponse(String response) {
                if (API.filterJson(response)) {
                    SendingOrderBean sendingOrderBean = JsonTwoUtils.parseJson(response, SendingOrderBean.class).get(0);

                    if (sendingOrderBean != null) {
                        sendMenuBeans.clear();
                        if (sendingOrderBean.list != null && sendingOrderBean.list.size() > 0) {
                            sendMenuBeans.addAll(sendingOrderBean.list);
                            check_out_tv.setEnabled(true);
                            check_out_tv.setBackground(context.getResources().getDrawable(R.drawable.shape_solid_red_5dp));
                            sending_listview.setVisibility(View.VISIBLE);
                        } else {
                            sending_listview.setVisibility(View.GONE);
                            check_out_tv.setEnabled(false);
                            check_out_tv.setBackground(context.getResources().getDrawable(R.drawable.shape_solid_not_enable_5dp));
                        }

                        sendingOrderAdapter.setList(sendMenuBeans);
                        total_count_tv.setText("总量：" + sendingOrderBean.count);
                        total_price_tv.setText("总价：" + sendingOrderBean.money);
                        showDialog();
                    }
                }
            }

            @Override
            public void onErrorResponse() {
                AtyUtils.showShort(context, "获取分店失败", true);
            }
        });
    }


    class SendingOrderAdapter extends ZmAdapter<SendingOrderBean.ListBean> {

        public SendingOrderAdapter(Context mContext, List<SendingOrderBean.ListBean> itemList) {
            super(mContext, itemList);
        }

        @Override
        public void init(ZmHolder zmHolder, final SendingOrderBean.ListBean sendingOrderBean, int position) {
            TextView food_name = zmHolder.getView(R.id.food_name);
            TextView food_pirce = zmHolder.getView(R.id.food_pirce);
            TextView food_state = zmHolder.getView(R.id.food_state);
            TextView confirm_sned = zmHolder.getView(R.id.confirm_sned);
            TextView food_gift = zmHolder.getView(R.id.food_gift);

            food_name.setText(sendingOrderBean.title);
            food_pirce.setText("¥:" + sendingOrderBean.price + "  x" + sendingOrderBean.nums);

            if (sendingOrderBean.staus_give == 1) {
                food_state.setText("已送达");
                confirm_sned.setVisibility(View.GONE);
            } else if (sendingOrderBean.staus_give == 0) {
                food_state.setText("未送达");

                confirm_sned.setVisibility(sendingOrderBean.is_auto_send == 0 ? View.VISIBLE : View.GONE);
            }

            food_name.setTextColor(sendingOrderBean.staus_give == 1 ? context.getResources().getColor(R.color.main_menu_isopen_color) :
                    context.getResources().getColor(R.color.login_bt_color));
            food_pirce.setTextColor(sendingOrderBean.staus_give == 1 ? context.getResources().getColor(R.color.main_menu_isopen_color) :
                    context.getResources().getColor(R.color.login_bt_color));
            food_state.setTextColor(sendingOrderBean.staus_give == 1 ? context.getResources().getColor(R.color.main_menu_isopen_color) :
                    context.getResources().getColor(R.color.login_bt_color));

            food_gift.setVisibility(sendingOrderBean.status == 5 || sendingOrderBean.status == 7 ? View.VISIBLE : View.GONE);

            confirm_sned.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCallbackListener.onCallback(1, sendingOrderBean.salt);
                }
            });
        }

        @Override
        public void setList(List<SendingOrderBean.ListBean> itemList) {
            super.setList(itemList);
            notifyDataSetChanged();
        }

        @Override
        public int setLayout() {
            return R.layout.item_dialog_sending_order;
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_sending_dis:
                break;
            case R.id.check_out_tv:
                onCallbackListener.onCallback(0, sendMenuBeans.size() > 0 ? sendMenuBeans.get(0).order_sn : "");
                break;
        }
        dismissDialog();
    }
}
